﻿/* Autoduel Game
 * CS4911 Senior Design Project
 * Fall 2010 Semester
 * Ian Axelrod, Liz Ha, Andrew Muldowney, Edward Futch
 */
using System;
using System.Text;

namespace AutoduelClient.GameClient.Network
{
    /// <summary>
    /// Provides various utility functions
    /// related to the client's networking
    /// </summary>
    public static class Util
    {
		#region Static fields (1)
		/// <summary>
        /// The delimiter used for packets
        /// </summary>
        public const byte PKT_DLM = (byte)'%';
		#endregion

		#region Static methods (2)
		/// <summary>
        /// Takes a packet at a particular index and converts the data
        /// represented into a string, until it hits the defined delimiter
        /// The index is advanced to the index after the delimiter
        /// </summary>
        /// <param name="packet">The packet to convert</param>
        /// <param name="index">The index to start the conversion at</param>
        /// <returns>The string</returns>
        public static string MakeString(byte[] packet, ref int index)
        {
            StringBuilder str = new StringBuilder();

            while (packet[index] != Util.PKT_DLM)
            {
                str.Append((char)packet[index]);
                index++;
            }
            index++;

            return str.ToString();
        }

		/// <summary>
        /// Reverses the bytes representing an integer
        /// </summary>
        /// <param name="theInt">The integer to reverse</param>
        /// <returns>The reversed integer</returns>
        public static int ReverseInt32(int theInt)
        {
            byte[] revInt = BitConverter.GetBytes(theInt);
            
            Array.Reverse(revInt);
            return BitConverter.ToInt32(revInt, 0);
        }
		#endregion
    }
}
